import time

from qiniu import Auth, put_file, etag
import qiniu.config
import requests
from common.models import rds, db, cache

# 需要填写你的 Access Key 和 Secret Key
access_key = 'eF4FbTEmJgappCTh005ZhnLRUCSFuaMSdsRq6IJy'
secret_key = 'lhYcCubuejZfMWplnR1zzSal_R1NqckDw6GagEOI'


def upload():
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 要上传的空间
    bucket_name = 'wangyufei911'
    # 上传后保存的文件名
    key = 'test/aaa.py'
    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)
    # 要上传文件的本地路径
    localfile = './main.py'
    ret, info = put_file(token, key, localfile)
    print(info)
    print(ret)
    assert ret['key'] == key
    assert ret['hash'] == etag(localfile)


def download():
    """

    """
    q = Auth(access_key, secret_key)
    base_url = 'http://%s/%s' % ('wangyufei911', 'test/aaa.py')
    private_url = q.private_download_url(base_url, expires=3600)
    print(private_url)
    r = requests.get(private_url)
    assert r.status_code == 200


def test_redis():
    redis_client = rds.pipeline()
    for i in range(10):
        redis_client.set(i, i)
        time.sleep(5)
    redis_client.execute()


# test_redis()
